package grow

import (
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/errors"
	"server/game/cache"
	"server/game/model"
)

/**
* @name setRestArea
* @param string data  休息区数据
* 更新休息区
 */

func (this *unit) SetRestArea(c *context.Context) any {
	data := c.GetString("data")
	if len(data) > 10240 {
		return errors.New("restArea too large")
	}
	role := cache.GetRole(c.Player.Updater)
	role.Set("restArea", data)
	return nil
}

/**
* @name setBattleArea
* @param string data
* 更新战斗区
 */
func (this *unit) SetBattleArea(c *context.Context) any {
	data := c.GetString("data")
	if len(data) > 10240 {
		return errors.New("battleArea too large")
	}
	role := cache.GetRole(c.Player.Updater)
	role.Set("battleArea", data)
	return nil
}

/**
* @name loveUnit
* @param string key 唯一ID
* @param int love 0是不爱1是爱
* 设置单位喜欢状态
 */

func (this *unit) LoveUnit(c *context.Context) any {
	key := c.GetString("key")
	love := c.GetInt32("love")
	if key == "" {
		return errors.ErrArgEmpty
	}
	if love != 0 {
		love = 1
	}

	items := cache.GetItems(c.Player.Updater)
	data := items.Get(key)
	if data == nil {
		return errors.ErrDataNotExists
	}
	if data.Attach.GetInt32(model.AttachUnitLove) == love {
		return nil
	}
	items.SetAttach(key, model.AttachUnitLove, love)
	return nil
}
